
#include <QPainter>
#include "toolselection.h"
#include "log.h"

ToolSelection::ToolSelection() : EditorTool("selection"),
	m_pressed(false)
{
}

ToolSelection::~ToolSelection()
{
}

void ToolSelection::mousePress(const QPoint& p)
{
	m_pressed = true;
	m_startPoint = p;
}

void ToolSelection::mouseRelease(const QPoint& p)
{
	m_pressed = false;
	m_endPoint = p;
	if((m_endPoint - m_startPoint).manhattanLength() < 2)
		m_endPoint = m_startPoint;

}

void ToolSelection::mouseMove(const QPoint& p)
{
	if(m_pressed)
	{
		widget()->update();
		m_endPoint = p;
	}
}

QRect ToolSelection::draw(QPainter* p)
{
	if(m_endPoint == m_startPoint)
		return QRect();
	p->save();
	QPen mypen = QPen(QColor(0, 0, 0));
	QRect r = QRect(m_startPoint, m_endPoint);
	mypen.setStyle(Qt::DotLine);
	p->setPen(mypen);
	p->drawRect(r);
	p->restore();
	r.adjust(-1, -1, 1, 1);
	return r;
}

